package game.tileMap
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class GameCamera
	{
		public var x:Number;
		public var y:Number;
		public var width:uint;
		public var height:uint;
		
		public var deadzone:Rectangle
		public var target:DisplayObject;
		public var scroll:Point;
		/**
		 *边界限制 
		 */		
		public var bounds:Rectangle;
		public var stage:DisplayObject;
		public function GameCamera(X:int,Y:int,Width:int,Height:int,Zoom:Number=0)
		{
			x = X;
			y = Y;
			width = Width;
			height = Height;
			scroll=new Point(0,0);
			target = null;
			deadzone = null;
		}
		
		public function update():void
		{
			//Either follow the object closely, 
			//or doublecheck our deadzone and update accordingly.
			if(target != null){
				if(deadzone == null){
					focusOn(new Point(target.x,target.y));
				}else{
					var t:Number;
					t = target.x - deadzone.x;
					if(scroll.x > t)
						scroll.x = t;
					t = target.x  - deadzone.x - deadzone.width;
					if(scroll.x < t)
						scroll.x = t;
					
					t = target.y - deadzone.y;
					if(scroll.y > t)
						scroll.y = t;
					t = target.y - deadzone.y - deadzone.height;
					if(scroll.y < t)
						scroll.y = t;
				}
			}
		
			//Make sure we didn't go outside the camera's bounds
			if(bounds != null)
			{
				if(scroll.x < bounds.left)
					scroll.x = bounds.left;
				if(scroll.x > bounds.right - width)
					scroll.x = bounds.right - width;
				if(scroll.y < bounds.top)
					scroll.y = bounds.top;
				if(scroll.y > bounds.bottom - height)
					scroll.y = bounds.bottom - height;
			}
			
			if (stage){
				stage.x=-scroll.x;
				stage.y=-scroll.y;
			}
			
			
		}
		
		
		public function follow(Target:DisplayObject, Style:uint=0):void
		{
			target = Target;
			var d:Number;
			switch(Style)
			{
				case 1:
					var w:Number = width/8;
					var h:Number = height/3;
					//deadzone = new Rectangle((width-w)/2,(height-h)/2 - h*0.25,w,h);
					
					deadzone = new Rectangle((width-w)/2,20,w,150);
					break;
				case 2:
					d =Math.max(width,height)/4;
					deadzone = new Rectangle((width-d)/2,(height-d)/2,d,d);
					break;
				case 3:
					d = Math.max(width,height)/8;
					deadzone = new Rectangle((width-d)/2,(height-d)/2,d,d);
					break;
				case 0:
				default:
					deadzone = null;
					break;
			}
		}
		
		/**
		 * Move the camera focus to this location instantly.
		 * 
		 * @param	Point		Where you want the camera to focus.
		 */
		public function focusOn(point:Point):void
		{
			//scroll=point.clone();
			scroll=new Point(point.x - (width>>1),point.y - (height>>1));
		}
		
		/**
		 * Specify the boundaries of the level or where the camera is allowed to move.
		 * 
		 * @param	X				The smallest X value of your level (usually 0).
		 * @param	Y				The smallest Y value of your level (usually 0).
		 * @param	Width			The largest X value of your level (usually the level width).
		 * @param	Height			The largest Y value of your level (usually the level height).
		 */
		public function setBounds(X:Number=0, Y:Number=0, Width:Number=0, Height:Number=0):void
		{
			if(bounds == null)
				bounds = new Rectangle(X,Y,Width,Height);
			update();
		}
		
		
	}
}